<?php
  if (!isset($_GET['Typo'])) {
  	$_GET['Typo'] = 3;
	}
  if (!isset($_GET['Group'])) { 
  	$Group = 'Office';} else {$Group = $_GET['Group'];
 	 }
  if (!isset($_GET['Group2'])) { 
  	$Group2 = 1;} else {$Group2 = $_GET['Group2'];
 	 }
  $TimeType = 'TimeClose';
  $OLDPAR = 0;
  $TOTAL = '0.00';
  $SUBTOTAL = '0.00';
  ?>
<div style="padding:10px">
<table width="100%" border="0">
  <tr>
    <td colspan="3"><div align="center"></div>      
      <div align="center">Designação</div></td>
    <td><div align="center">Sub-Total</div></td>
    <td><div align="center">Total</div></td>
  </tr>
  <?php
	
	$Results = $GLOBALS['MYD']->db_array("SELECT 
					h.name AS Office,
					j.name AS User,
					g.name AS CODE,
					g.CODE AS COD,
					g.Type AS CODTYPE,
					g.ESCODE AS PCOD,
					l.Ref,
					l.Description,
					SUM(a.Amount) AS Exp,
					c.icon,
					k.name AS Provider
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				INNER JOIN ccenter_contas AS g ON LEFT(g.CODE, LENGTH(b.CODE)) = b.CODE OR LEFT(g.CODE, LENGTH(a.CODE)) = a.CODE
				INNER JOIN hcce_pocothers AS l ON l.Ref = LEFT(g.ESCODE,2)
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				LEFT OUTER JOIN main_users AS j ON j.ref = a.User
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref IN (a.Provider, b.RefProvider)
				INNER JOIN main_settings_currency AS c ON c.ref = b.RefCurrency
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_POST['Time']}' AND '{$_POST['TimeEnd']}'
				AND b.Status = 2
				AND a.Status != 5
				GROUP BY l.Ref
				ORDER BY l.Ref ASC"); 
	for ($f = 1; $f <= $Results[0]; $f++) {
		
	$TOTAL = $TOTAL+$Results[$f]['Exp']; 
	echo '<tr style="height:26;background-color:'.gridlines($f).'">
    <td colspan="3"><b>'.strtoupper($Results[$f]['Description']).'</b></td>
		<td colspan="2" align="right"><b>'.$Results[$f]['Exp'].'</b></td>
		</tr>';
	
     $ESG = $GLOBALS['MYD']->db_array("SELECT 
					h.name AS Office,
					j.name AS User,
					g.name,
					g.CODE AS COD,
					g.Type AS CODTYPE,
					g.ESCODE AS PCOD,
					SUM(a.Amount) AS SUB,
					c.icon,
					k.name AS Provider
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				INNER JOIN ccenter_contas AS g ON LEFT(g.CODE, LENGTH(b.CODE)) = b.CODE OR LEFT(g.CODE, LENGTH(a.CODE)) = a.CODE
				INNER JOIN hcce_pocothers AS l ON l.Ref = LEFT(g.ESCODE,2)
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				LEFT OUTER JOIN main_users AS j ON j.ref = a.User
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref IN (a.Provider, b.RefProvider)
				INNER JOIN main_settings_currency AS c ON c.ref = b.RefCurrency
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_POST['Time']}' AND '{$_POST['TimeEnd']}'
				AND b.Status = 2
				AND a.Status != 5
				AND l.Ref = {$Results[$f]['Ref']}
				GROUP BY g.ESCODE");
	  for ($y = 1; $y <= $ESG[0]; $y++) { 
	  
	if ($ESG[$y]['CODTYPE'] == 1) {
  ?>
  <tr>
    <td colspan="3"><strong><?php echo $ESG[$y]['COD'];?></strong></td>
    <td colspan="2" align="right"><strong><?php echo $ESG[$y]['SUB'];?>
    </strong></td>
  </tr>
  <?php
  } elseif ($ESG[$y]['CODTYPE'] == 0) { ?>
  <tr>
    <td align="right"><b><?php echo $ESG[$y]['COD'];?></b>
      <div align="right"></div>
    <div align="right"></div></td>
    <td width="46%"><?php echo $ESG[$y]['name'];?></td>
    <td align="right">&nbsp;</td>
    <td width="15%" align="right"><?php echo $ESG[$y]['SUB'];?></td>
    <td width="15%" align="right">&nbsp;</td>
  </tr>
  <?php		}
  	} 
	}?>
    <tr>
    <td colspan="4">Total:</td>
    <td align="right"><b>� <?php echo $TOTAL;?></b></td>
  </tr>
</table>
</div>